package com.zbkj.admin.controller.publicly;

import com.zbkj.admin.service.WeWorkService;
import com.zbkj.dal.base.result.CommonResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Api(value = "企业微信相关接口")
@RestController
@RequestMapping("/api/admin/v2/we-work")
public class WeWorkController {

    @Resource
    private WeWorkService weWorkService;

    @GetMapping(value = "/books/notify")
    public void booksNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String msgSignature = request.getParameter("msg_signature");
        String echostr = request.getParameter("echostr");
        String nonce = request.getParameter("nonce");
        String timestamp = request.getParameter("timestamp");
        log.info("企微通知：msg_signature:{},echostr:{},nonce:{},timestamp:{}", msgSignature, echostr, nonce, timestamp);
        String result = weWorkService.booksNotify(msgSignature, echostr, nonce, timestamp);
        response.getWriter().print(result);
    }

    @PostMapping(value = "/books/notify")
    public void booksNotifyPost(HttpServletRequest request, @RequestBody String data) {
        String msgSignature = request.getParameter("msg_signature");
        String echostr = request.getParameter("echostr");
        String nonce = request.getParameter("nonce");
        String timestamp = request.getParameter("timestamp");
        log.info("Post企微通知：msg_signature:{},echostr:{},nonce:{},timestamp:{}", msgSignature, echostr, nonce, timestamp);
        log.info("Post data;{}", data);
        weWorkService.userUpdateNotify(msgSignature, timestamp, nonce, data);
    }

    @GetMapping("/init/employee")
    public CommonResult<Void> initEmployee() {
        weWorkService.initEmployee();
        return CommonResult.success();
    }

    @PostMapping("/add/employee")
    public CommonResult<Void> addEmployee(@RequestParam("weWorkUserId") String weWorkUserId) {
        weWorkService.addEmployee(weWorkUserId);
        return CommonResult.success();
    }


}
